15 Deploy lagcishiny on your server
15.1 Before starting
You need to prepare the following things: - A server with a public IP address - Domain name with DNS record - non-root account, such as ubuntu
- Opening ufw
In this tutorial, We use Ubuntu 22.04
.
sudo ufw allow OpenSSH
sudo ufw allow "Nginx Full"
sudo ufw enable
sudo ufw status
sudo apt install -y nginx
sudo apt install -y certbot python3-certbot-nginx
15.1.1 Install R
sudo apt update -qq
sudo apt install --no-install-recommends -y software-properties-common dirmngr
wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc \
| sudo tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
gpg --show-keys /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
# Fingerprint should be: E298A3A825C0D65DFD57CBB651716619E084DAB9
sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/"
sudo apt update
sudo apt install --no-install-recommends -y r-base
# (optional, needed for compiling packages from source)
sudo apt install -y r-base-dev
Check it.
R --version
15.2 Shiny Server + Nginx
15.2.1 Install Shiny Server
sudo apt update && sudo apt upgrade -y
# Install R from Ubuntu repo (basic)
sudo apt install -y r-base gdebi-core
# Or (recommended) add CRAN repo for latest R
sudo apt install -y software-properties-common dirmngr apt-transport-https
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 51716619E084DAB9
sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/"
sudo apt update
sudo apt install -y r-base
sudo su - \
"R -e \"install.packages('shiny', repos='https://cloud.r-project.org/')\"" -c
# Download the latest .deb (for Ubuntu 22.04, use bionic build)
wget https://download3.rstudio.org/ubuntu-14.04/x86_64/shiny-server-1.5.20.1002-amd64.deb
# Install with gdebi
sudo gdebi shiny-server-1.5.20.1002-amd64.deb
sudo systemctl enable shiny-server
sudo systemctl start shiny-server
sudo systemctl status shiny-server
Check it:
curl -I http://localhost:3838
15.2.2 Deploy Shiny APP
sudo mkdir -p /srv/shiny-server/app1
sudo chown -R shiny:shiny /srv/shiny-server/app1
sudo -u shiny bash -lc 'R -q -e "sessionInfo()"'
# copy your code of shiny app
sudo cp -r ~/your_local_app/* /srv/shiny-server/app1/
sudo chown -R shiny:shiny /srv/shiny-server/app1
Then you can access the app via curl -I http://your_server_ip:3838/app1
15.2.3 Configure Nginx
Create a new Nginx site configuration (taking the root path mapping to Shiny Server as an example, /app1 is the application path) :
sudo vim /etc/nginx/sites-available/shiny.conf
Insert these content:
# /etc/nginx/sites-available/shiny.conf
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
server_name yourdomain.com; # modify to your domain name
# limit size of uploaded files (Shiny upload)
client_max_body_size 200m;
# 静态资源缓存(可选,小心不要缓存动态内容)
location ~* \.(css|js|png|jpg|jpeg|gif|svg|ico)$ {
expires 30d;
add_header Cache-Control "public";
root /var/www/html; # 若无独立静态资源,可删掉本段
}
# Reverse proxy to Shiny Server (3838)
location / {
proxy_pass http://127.0.0.1:3838;
proxy_redirect off;
# WebSocket need
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
# Convey genuine source information
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# Shiny requests can be very "long"
proxy_read_timeout 3600s;
proxy_send_timeout 3600s;
send_timeout 3600s;
}
}
Enable and check the configuration:
sudo ln -s /etc/nginx/sites-available/shiny.conf /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Now, you can access your shiny app via:http://yourdomain.com/app1
15.2.4 Get HTTPS certification
sudo certbot --nginx -d yourdomain.com
Select 1
Automatically redirect to HTTPS
15.3 Docker + Nginx
15.3.1 Download docker image
docker pull jaspershenlab/laggedcorshiny:latest
docker run -p 3838:3838 -v /Path/In/Your/Server:/home jaspershenlab/laggedcorshiny:latest
15.3.2 Nginx
The following configuration is the same as that of Shiny Server + Nginx
part.